﻿#ifndef COMMON_H
#define COMMON_H
#include <IMVAPI/IMVApi.h>
#include <stdexcept>
#include <array>
#include <QHash>
#include <opencv2/core/mat.hpp>

#define WORKNUM 2

enum DetectionRsult
{
    OK,
    HXAREA_Err,
    HXSIZE_H_Err,
    HXSIZE_W_Err,
    YJ_Err,
    HXPY_Err,
    TBDIS_Err,
    LT_Err,
    HD_Err,
    NG,
};

//enum HXDetectionRsult
//{
//    HXSIZE_Err,
//    HXAREA_Err,
//    HXPY_Err,
//    TBDIS_Err,
//};

struct Params
{
    std::string weldingdetectorfile;
    float conf_thresh;
    float nms_thresh;
    std::string Seg_weldingdetectorfile;
    float Seg_conf_thresh;
    float Seg_nms_thresh;
};

struct InSolderParam
{
    //融合相关参数
    float gray_param;
    int splitsize;
};

struct InThresholdParam
{
    //尺寸阈值
    float min_w = 2;
    float min_h = 2;
    float max_w = 100;
    float max_h = 100;
    //面积阈值
    float min_area = 3;
    float max_area = 80;
    //铜板距离阈值
    float max_TBDis = 3;
    float min_TBDis = 0.01;//最小没要求，目前预留一个参数
    //焊锡偏离程度
    float max_PY = 4.1;
    //相机标定
    float pix2mmScale_F_x = 0.010526;
    float pix2mmScale_F_y = 0.010526;
    float pix2mmScale_Z_x = 0.011765;
    float pix2mmScale_Z_y = 0.001765;
    bool AutoBDswitch = 0;
    //瑕疵检测
    float HD_min = 1;
    //黑点和露铜面积控制
    float LT_ALLArea_max = 0.15;
    float LT_MAXArea_max = 0.2;
    float HD_ALLArea_max = 12;
    float HD_MAXArea_max = 8;
};
struct HXResult
{
    int hxid;//焊锡编号
    float hx_weight;//焊锡长
    float hx_height;//焊锡宽
    float TBtoHX_distance;//裸露铜引脚长度
    float Pixeldis;//用于显示
    float center_pianyi;
    float PixelPY;//Y=用于显示
    //焊点检测框坐标
    float x1, y1, x2, y2;
};

struct TBResult
{
    //引脚检测框坐标
    float x1, y1, x2, y2;
    float YJ_H, YJ_W;
};

struct WeldingResult
{
    int id;
    DetectionRsult NO_HX_NGReults;
    cv::Mat FusionMap;
    cv::Mat dst;
    cv::Rect weldingrect;
    float distance;
    std::vector<HXResult> hxResult;
    bool NG_OKflag = false;
    //瑕疵面积
    double HX_Area = 0;
    double HD_SumArea=0, HD_MaxArea=0;
    double LT_SumArea=0, LT_MaxArea=0;
};


struct DB_WeldingResult{
    int id;//序号
    QString hxPort;//端口
    int hxId;//焊锡编号
    int hxBatch; //焊锡批次
    float hxWeight=0;//焊锡长
    float hxHeight=0;//焊锡宽
    double hxArea = 0; //瑕疵面积
    float hxDistance=0;//裸露铜引脚长度
    float hxPianyi=0;    //焊锡偏移
    double hxLTSumArea=0; //漏铜面积
    double hxLTMaxArea=0; //漏铜最大面积
    double hxHDSumArea=0; //黑点面积
    double hxHDMaxArea=0; //黑点最大面积
    bool hxReult = false; //检测结果
    QString hxReultsDescribe; //结果说明
    QString createTime;
    QString changeTime;

    DB_WeldingResult() = default;
};

struct CFrameResult
{
    int sig;
    int camera;
    bool result;
};

struct CameraSw {
    int m_nSource;
    int m_nComposite;
    int m_nOK;
    int m_nNG;
    CameraSw(int ng = 0, int ok = 0, int composite = 0, int source = 0)
        : m_nSource(source), m_nComposite(composite), m_nOK(ok), m_nNG(ng) {}
};

struct SaveSw
{
    CameraSw m_cSave1, m_cSave2, m_cSave3, m_cSave4, m_cSave5, m_cSave6;

    CameraSw& getCameraSw(int index) {
        switch(index) {
        case 1: return m_cSave1;
        case 2: return m_cSave2;
        case 3: return m_cSave3;
        case 4: return m_cSave4;
        case 5: return m_cSave5;
        case 6: return m_cSave6;
        default: throw std::out_of_range("Invalid group index");
        }
    }
};
// 成员指针类型别名
using CameraSwMember = int CameraSw::*;

inline constexpr std::array<CameraSwMember, 4> cameraSwMembers = {
    &CameraSw::m_nSource,
    &CameraSw::m_nComposite,
    &CameraSw::m_nOK,
    &CameraSw::m_nNG
};

// 帧信息
// frame imformation
class CFrameInfo
{
public:
    CFrameInfo()
    {
        m_pImageBuf = NULL;
        m_nBufferSize = 0;
        m_nWidth = 0;
        m_nHeight = 0;
        m_ePixelType = gvspPixelMono8;
        m_nPaddingX = 0;
        m_nPaddingY = 0;
        m_nTimeStamp = 0;
    }

    ~CFrameInfo()
    {
    }

public:
    QString cameraKey;
    std::shared_ptr<unsigned char> m_pImageBuf;
    int				m_nBufferSize;
    int				m_nWidth;
    int				m_nHeight;
    IMV_EPixelType	m_ePixelType;
    int				m_nPaddingX;
    int				m_nPaddingY;
    uint64_t		m_nTimeStamp;
};
#endif // COMMON_H
